package com.example.petproject.service.impl;

import com.example.petproject.entity.DetailCategories;
import com.example.petproject.entity.DetailSkuList;
import com.example.petproject.entity.DetailSpu;
import com.example.petproject.entity.dto.ProductSearchDTO;
import com.example.petproject.entity.vo.DetailVO;
import com.example.petproject.entity.vo.PageResult;
import com.example.petproject.entity.vo.ProductSearchVO;
import com.example.petproject.entity.vo.ReturnStatusEnum;
import com.example.petproject.execption.BaseException;
import com.example.petproject.mapper.PmsSpuMapper;
import com.example.petproject.service.PmsProductService;
import com.example.petproject.service.third.ali.AliYunOSSService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 包名称：com.example.petproject.service.impl
 * 类名称：PmsProductServiceImpl
 * 类描述：商品实现
 * 创建人：@author DrinkMore-HotWater
 * 创建时间：2024年01月12日 15:52
 **/
@Service
public class PmsProductServiceImpl implements PmsProductService {
    @Resource
    private PmsSpuMapper pmsSpuMapper;
    @Resource
    private AliYunOSSService aliYunOSSService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取商品搜索信息
     *
     * @return ProductSearchVO
     */
    @Override
    public PageResult<ProductSearchVO> getSearch(ProductSearchDTO productSearchDTO) {
        PageHelper.startPage(productSearchDTO.getPageNo(), productSearchDTO.getPageSize());
        Page<ProductSearchVO> page = pmsSpuMapper.selectProductByParam(productSearchDTO);
        page.getResult().forEach(item -> {
            //先从redis取出临时url
            String defaultPicUrl=item.getDefaultPicUrl();
            if (!ObjectUtils.isEmpty(defaultPicUrl)) {
                //如果key存在，则从redis中取出  TODO 可以考虑封装一个redis相关的工具类
                defaultPicUrl=stringRedisTemplate.hasKey(defaultPicUrl)?
                        stringRedisTemplate.opsForValue().get(defaultPicUrl):
                        aliYunOSSService.getSymink(item.getDefaultPicUrl());
                item.setDefaultPicUrl(defaultPicUrl);
            }
        });
        return PageResult.ok(page);
    }

    /**
     * 查看商品详细
     * @param spuId 商品id
     * @return List<DetailVO>
     */
    @Override
    public DetailVO getDetail(Integer spuId) {
        DetailVO detailVO = new DetailVO();
        //根据商品id查询商品详细
        DetailSpu spu = pmsSpuMapper.selectBySpuId(spuId);
        if (ObjectUtils.isEmpty(spu)) {
            throw new BaseException(ReturnStatusEnum.PRODUCT_SPU_EMPTY);
        }
        //查询商品分类
        List<DetailCategories> categories = pmsSpuMapper.selectCategories();
        //查询商品的sku列表
        List<DetailSkuList> skuList = pmsSpuMapper.selectSkuListBySpuId(spuId);
        //设置商品图片
        spu.getProductDetailPicList().forEach(item -> item.setPicUrl(aliYunOSSService.getSymink(item.getPicUrl())));
        //设置商品图片
        skuList.forEach(item -> item.getSkuPicList().forEach(pic -> pic.setPicUrl(aliYunOSSService.getSymink(pic.getPicUrl()))));
        detailVO.setSpu(spu);
        detailVO.setCategories(categories);
        detailVO.setSkuList(skuList);
        return detailVO;
    }
}
